package com.oss.controller.system;

import com.oss.common.BaseResponse;
import com.oss.dto.rights.*;
import com.oss.entity.system.Rights;
import com.oss.entity.system.Token;
import com.oss.service.system.RightsService;
import com.oss.service.system.TokenService;
import com.oss.vo.rights.RightsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author chengqiang
 */
@RestController
@RequestMapping("/rights")
public class RightsController {
    @Autowired
    RightsService rightsService;
    @Autowired
    TokenService tokenService;

    /**
     * 新增权限
     * 权限代码唯一
     * @param dto 入参
     * @return 返回提示信息
     */
    @RequestMapping("/addRights")
    public BaseResponse<String> addRights(@RequestBody @Valid RightsAddDto dto){
        return rightsService.addRights(dto);
    }
    /**
     * 删除权限
     * @param dto 入参
     * @return 返回提示信息
     */
    @RequestMapping("/deleteRights")
    public BaseResponse<String> deleteRights(@RequestBody @Valid RightsDeleteDto dto){
        return BaseResponse.success(rightsService.deleteRights(dto));
    }
    /**
     * 修改权限
     * @param dto 入参
     * @return 返回提示信息
     */
    @RequestMapping("/updateRights")
    public BaseResponse<String> updateRights(@RequestBody @Valid RightsUpdateDto dto){
        return rightsService.updateRights(dto);
    }
    /**
     * 获取某个权限详情
     * @param dto 入参
     * @return 返回提示信息
     */
    @RequestMapping("/getRightsById")
    public BaseResponse<RightsVO> getRightsById(@RequestBody @Valid RightsGetOneDto dto){
        return BaseResponse.success(rightsService.getRightsById(dto.getRightsId()));
    }
    /**
     * 权限查询(角色授权使用)
     * @param dto 入参
     * @return 返回提示信息
     */
    @RequestMapping("/lisRights")
    public BaseResponse lisRights(@RequestBody @Valid RightsListDto dto){
        dto.setMenuSelect(0);
        return rightsService.listAll(dto);
    }
    /**
     * 菜单查询
     * @param dto 入参
     * @return 返回提示信息
     */
    @RequestMapping("/listAll")
    public BaseResponse listAll(@RequestBody @Valid RightsListDto dto){
        dto.setMenuSelect(1);
        return rightsService.listAll(dto);
    }
    /**
     * 获取某个用户的权限列表
     * @return 返回提示信息
     */
    @RequestMapping("/getRightsByUserId")
    public BaseResponse<List<Rights>> getRightsByUserId(HttpServletRequest request) throws Exception {
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        Token tokenObejct = tokenService.getById(token);
        if (tokenObejct == null){
            throw new Exception("token不存在");
        }
        String userId = tokenObejct.getUserId();
        return BaseResponse.success(rightsService.getRightsByUserId(userId));
    }

    /**
     * 获取所有可选的父级权限
     * @return 返回权限列表
     */
    @RequestMapping("/getParentRightsList")
    public BaseResponse<List<RightsVO>> getParentRightsList() {
        return BaseResponse.success(rightsService.getParentRightsList());
    }
}
